import filters from './filter'
const utils = {
  ...filters,


  numInt(v,old){

    console.log(v)
   

    if(v>0){
      if(v.indexOf('.')!=-1){
        setTimeout(()=>{
          console.log(old)
          return  old
        },0)
      }else{
        console.log(v)
        return v
      }
    }else{
      
      console.log(v)
        return  ''
     
    }

  },

  getTime: (time, type = '-') => {
    if (!time) return ''
    const date = new Date(time);
    const formatObj = {
      y: date.getFullYear(),
      m: date.getMonth() + 1,
      d: date.getDate(),
      h: date.getHours(),
      i: date.getMinutes(),
      s: date.getSeconds(),
      a: date.getDay()
    }

    let { y, m, d } = formatObj;
    m = m < 10 ? '0' + m : m;
    d = d < 10 ? '0' + d : d;

    return `${y}${type}${m}${type}${d}`

  },

// 品类
  kind:{
    list:[
      {
        id:1,
        name:'活畜类'
      },
      {
        id:2,
        name:'肉类'
      },
      {
        id:3,
        name:'蛋类'
      },
      {
        id:4,
        name:'奶类'
      },
      {
        id:5,
        name:'饲料类'
      },
      {
        id:6,
        name:'饲料添加剂类'
      } ,{
        id:7,
        name:'兽药类'
      },
      {
        id:8,
        name:'疫苗类'
      }
    ],
    getLabel:(id)=>{
      return utils.useType.list.find(ele=>ele.id==id).name
    }
  },


  
  	// int	是	用药方式id（明细）1 外敷 2 内服 3 针剂 4 点滴

    useType:{
      list:[
        {
          id:1,
          name:'外敷'
        },
        {
          id:2,
          name:'内服'
        },
        {
          id:3,
          name:'针剂'
        },
        {
          id:4,
          name:'点滴'
        }
      ],
      getLabel:(id)=>{
        return utils.useType.list.find(ele=>ele.id==id).name
      }
    },

  checkType:{
    list:[
      {
        id:1,
        name:'产地'
      },
      {
        id:2,
        name:'现场'
      },
      {
        id:3,
        name:'实验室'
      }
    ],
    getLabel:(id)=>{
      return utils.checkType.list.find(ele=>ele.id==id).name
    }
  },

  zlStatus:{
    list:[
      {
        id:0,
        name:'治疗中'
      },
      {
        id:1,
        name:'已治愈'
      },
      {
        id:2,
        name:'死淘'
      },
      {
        id:3,
        name:'活淘'
      }
    ],
    getLabel:(id)=>{
      return utils.zlStatus.list.find(ele=>ele.id==id).name
    }
  },


  zlStatusH:{
    list:[
      {
        id:1,
        name:'已治愈'
      },
      {
        id:2,
        name:'死淘'
      },
      {
        id:3,
        name:'活淘'
      }
    ],
    getLabel:(id)=>{
      return utils.zlStatus.list.find(ele=>ele.id==id).name
    }
  },
  


  // 时间处理函数
  getDate: (date) => {
    if (date) {
      let oldDate = new Date(date)  //将服务器获取的时间格式化
      // 获取当前时间
      let newDate = new Date()
      // 将新旧都转换成时间戳
      let startDate = newDate.getTime()
      let endDate = oldDate.getTime()
      // let endDate = date.getTime()
      // 获取天
      let day = parseInt((startDate - endDate) / 1000 / 3600 / 24)
      return day + '天'
    } else {
      return '--'
    }
  },



  // 数组对象Id去重
  setArr: (arr) => {

    let newArr = [];
    arr.forEach(ele => {
      newArr.findIndex(fin => fin.id == ele.id) == -1 && newArr.push(ele)
    })
    return newArr;
  },
  // 数字保留小数点后n位，默认2位
  numberSub: (num, old, n = 2) => {
    const numstring = String(num);
    if (num != old) {
      if (numstring.indexOf('.') != -1) {
        const len = numstring.split('.')[1].length;
        if (len > n) {
          return old;
        }
      }
      if (num < 0) {
        return ''
      }
    }
    if (num == '') {
      return ''
    }
    else {
      return num
    }

  },
  // 只能是1-100的整数
  numberPercent: (num, old) => {
    const numstring = String(num);
    if (num != old) {
      if (num > 100 || numstring.indexOf('.') != -1) {
        return old
      }
      if (num <= 0) {
        return ''
      }
    }
    return parseInt(num)
  },


  // 采精量

  // 100以内，并最多保留小数点后2位
  volume: (num, old, n = 2) => {
    if (num != old) {
      const numstring = String(num);
      if (numstring.indexOf('.') != -1) {
        const len = numstring.split('.')[1].length;
        if (len > n) {
          return old;
        }
      }
      if (num < 0) {
        return ''
      }
      if (num >= 100) {
        return 100
      }
    }
    if (num) {
      return num - 0
    } else {
     
      return ''
    }

  }



}

export default utils